$(function (){
   var num=location.href;
   //传过来是一个数字
   var nameIndex = num.indexOf("name=");
   let sqlName="";
   //判断这个数字不能为-1
    if (nameIndex!=-1){
        //截取
        sqlName=num.substring(nameIndex+5);
        //解码
        sqlName= decodeURI(sqlName);
        //条件打印
        console.log(sqlName);
    }
    //截取分类id
    let idIndex=num.indexOf("sid=");
    let sid;
    if (idIndex!=-1){
        //截取
        sid=num.substring(idIndex+4);
        //条件打印
        console.log(sid);
    }
    $.get(
        "productBySql",
        {sqlName:sqlName,sid:sid},//携带的数据
        function (msg){
            let product=msg.data;
            //循环
            for (let i = 0; i < product.length; i++) {
                let tr=$(`<div class="col-sm-3 product-item">
                               <div class="img-thumbnail">
                                   <div>
                                       <a href="product.html?pid=${product[i].id}">
                                            <img src="${product[i].url}" class="img-fluid rounded" />
                                       </a>
                                   </div>
                               </div>
                               <div class="title">
                                   <a href="products.html?pid=${product[i].id}">${product[i].name}</a>
                               </div>
                               <div class="price">
                                   <span>￥</span>
                                   <span>${product[i].price}</span>
                               </div>
                           </div>`);
                        $("#sp-div").append(tr);
            }
        },
        "json"
    )
});